//	Tic-Tac-Toe
//	By: Austin Noyes

#include <iostream>
#include <string>

using namespace std;

void PrintBoard();						// This function prints the Tic-Tac-Toe board, in original and updated versions
bool IsMoveLegal(char Move);			// Checks to see if the move is legal
char TurnDecider(char PlayerMark);		// This function decides what PlayerMark ('O' or 'X' is used)

char Tic1 = '1';				// These global character variables represent each place on the TTT board
char Tic2 = '2';
char Tic3 = '3';
char Tic4 = '4';
char Tic5 = '5';
char Tic6 = '6';
char Tic7 = '7';
char Tic8 = '8';
char Tic9 = '9';

int main()
{
	bool isGameOver = false;			// This boolean variable keeps the game (or function 'main' running)
	string Player1Name, Player2Name;	// These string variables grab the player's names and display them accordingly

	char PlayerMove;				// This is the player move that will be translated into the various places on the board once entered
	char PlayerMark;				// This character variable tells the compiler if it is P1 entering, == 'X' or P2 == 'O'

	cout << "\t\t\tWelcome to Tic-Tac-Toe!" << endl;
	cout << "\tIn this game, each of the two players will enter their name, then" << endl;
	cout << "\t\tPlayer 1 will be asked to enter his/her first guess." << endl;
	cout << "\tThen, the board will show up with each player's guess until the" << endl;
	cout << "\t\tthree-in-a-row is made out of the numbers!" << endl << endl;
	cout << "\t   Best of luck and may the best Tic-Tac-Toe'r win!" << endl << endl;

	cout << "Player 1, please enter your name: ";			// Gathers Player 1's name
	getline(cin, Player1Name);
	cout << endl << "Thank you, " << Player1Name << endl << endl;

	cout << "Player 2, please enter your name: ";			// Gathers Player 2's name
	getline(cin, Player2Name);
	cout << endl << "Thank you, " << Player2Name << endl << endl;

	PrintBoard();

	cout << Player1Name << ", please enter your move: ";	// Engage main game loop(?)
	cin >> PlayerMove;
	PlayerMark = 'X';

	while (isGameOver == false)								// Main game loop
	{
		if (IsMoveLegal(PlayerMove) == true)
		{
			if (PlayerMove == '1')
			{
				Tic1 = PlayerMark;
			}
			else if (PlayerMove == '2')
			{
				Tic2 = PlayerMark;
			}
			else if (PlayerMove == '3')
			{
				Tic3 = PlayerMark;
			}
			else if (PlayerMove == '4')
			{
				Tic4 = PlayerMark;
			}
			else if (PlayerMove == '5')
			{
				Tic5 = PlayerMark;
			}
			else if (PlayerMove == '6')
			{
				Tic6 = PlayerMark;
			}
			else if (PlayerMove == '7')
			{
				Tic7 = PlayerMark;
			}
			else if (PlayerMove == '8')
			{
				Tic8 = PlayerMark;
			}
			else if (PlayerMove == '9')
			{
				Tic9 = PlayerMark;
			}

			if (Tic1 == 'X' && Tic2 == 'X' && Tic3 == 'X')				// 1 - 2 - 3 combination (X)
			{
				cout << Player1Name << " wins!" << endl;
				isGameOver = true;
			}
			else if (Tic1 == 'X' && Tic5 == 'X' && Tic9 == 'X')			// 1 - 5 - 9 combination (X)
			{
				cout << Player1Name << " wins!" << endl;
				isGameOver = true;
			}
			else if (Tic1 == 'X' && Tic4 == 'X' && Tic7 == 'X')			// 1 - 4 - 7 combination (X)
			{
				cout << Player1Name << " wins!" << endl;
				isGameOver = true;
			}
			else if (Tic2 == 'X' && Tic5 == 'X' && Tic8 == 'X')			// 2 - 5 - 8 combination (X)
			{
				cout << Player1Name << " wins!" << endl;
				isGameOver = true;
			}
			else if (Tic3 == 'X' && Tic5 == 'X' && Tic7 == 'X')			// 3 - 5 - 7 combination (X)
			{
				cout << Player1Name << " wins!" << endl;
				isGameOver = true;
			}
			else if (Tic4 == 'X' && Tic5 == 'X' && Tic6 == 'X')			// 4 - 5 - 6 combination (X)
			{
				cout << Player1Name << " wins!" << endl;
				isGameOver = true;
			}
			else if (Tic3 == 'X' && Tic6 == 'X' && Tic9 == 'X')			// 3 - 6 - 9 combination (X)
			{
				cout << Player1Name << " wins!" << endl;
				isGameOver = true;
			}
			else if (Tic7 == 'X' && Tic8 == 'X' && Tic9 == 'X')			// 7 - 8 - 9 combination (X)
			{
				cout << Player1Name << " wins!" << endl;
				isGameOver = true;
			}

			if (Tic1 == 'O' && Tic2 == 'O' && Tic3 == 'O')				// 1 - 2 - 3 combination (O)
			{
				cout << Player2Name << " wins!" << endl;
				isGameOver = true;
			}
			else if (Tic1 == 'O' && Tic5 == 'O' && Tic9 == 'O')			// 1 - 5 - 9 combination (O)
			{
				cout << Player2Name << " wins!" << endl;
				isGameOver = true;
			}
			else if (Tic1 == 'O' && Tic4 == 'O' && Tic7 == 'O')			// 1 - 4 - 7 combination (O)
			{
				cout << Player2Name << " wins!" << endl;
				isGameOver = true;
			}
			else if (Tic2 == 'O' && Tic5 == 'O' && Tic8 == 'O')			// 2 - 5 - 8 combination (O)
			{
				cout << Player2Name << " wins!" << endl;
				isGameOver = true;
			}
			else if (Tic3 == 'O' && Tic5 == 'O' && Tic7 == 'O')			// 3 - 5 - 7 combination (O)
			{
				cout << Player2Name << " wins!" << endl;
				isGameOver = true;
			}
			else if (Tic4 == 'O' && Tic5 == 'O' && Tic6 == 'O')			// 4 - 5 - 6 combination (O)
			{
				cout << Player2Name << " wins!" << endl;
				isGameOver = true;
			}
			else if (Tic3 == 'O' && Tic6 == 'O' && Tic9 == 'O')			// 3 - 6 - 9 combination (O)
			{
				cout << Player2Name << " wins!" << endl;
				isGameOver = true;
			}
			else if (Tic7 == 'O' && Tic8 == 'O' && Tic9 == 'O')			// 7 - 8 - 9 combination (O)
			{
				cout << Player2Name << " wins!" << endl;
				isGameOver = true;
			}

			PrintBoard();
			PlayerMark = TurnDecider(PlayerMark);

			if (PlayerMark == 'O')
			{
				cout << Player2Name << ", please enter your move: ";
				cin >> PlayerMove;
			}
			else if (PlayerMark == 'X')
			{
				cout << Player1Name << ", please enter your move: ";
				cin >> PlayerMove;
			}
		}
		else
		{
			cout << "You entered an illegal move. Please re-enter: ";
			cin >> PlayerMove;
		}
	}
	system("PAUSE");
	return 0;
}

void PrintBoard()
{
	cout << "   " << Tic1 << "|" << Tic2 << "|" << Tic3 << endl;
	cout << "   - - -" << endl;
	cout << "   " << Tic4 << "|" << Tic5 << "|" << Tic6 << endl;
	cout << "   - - -" << endl;
	cout << "   " << Tic7 << "|" << Tic8 << "|" << Tic9 << endl << endl;
}

bool IsMoveLegal(char Move)
{
	char PlayerMove;
	PlayerMove = Move;

	if (PlayerMove == '1' || PlayerMove == '2' || PlayerMove == '3' ||
		PlayerMove == '4' || PlayerMove == '5' || PlayerMove == '6' ||
		PlayerMove == '7' || PlayerMove == '8' || PlayerMove == '9')
	{
		if (PlayerMove == '1' && Tic1 == '1')
		{
			return true;
		}
		else if (PlayerMove == '2' && Tic2 == '2')
		{
			return true;
		}
		else if (PlayerMove == '3' && Tic3 == '3')
		{
			return true;
		}
		else if (PlayerMove == '4' && Tic4 == '4')
		{
			return true;
		}
		else if (PlayerMove == '5' && Tic5 == '5')
		{
			return true;
		}
		else if (PlayerMove == '6' && Tic6 == '6')
		{
			return true;
		}
		else if (PlayerMove == '7' && Tic7 == '7')
		{
			return true;
		}
		else if (PlayerMove == '8' && Tic8 == '8')
		{
			return true;
		}
		else if (PlayerMove == '9' && Tic9 == '9')
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

char TurnDecider(char PlayerMark)
{
	int xCount = 0;
	int oCount = 0;
	char *CharPtr1;
	char *CharPtr2;
	char *CharPtr3;
	char *CharPtr4;
	char *CharPtr5;
	char *CharPtr6;
	char *CharPtr7;
	char *CharPtr8;
	char *CharPtr9;

	CharPtr1 = &Tic1;
	CharPtr2 = &Tic2;
	CharPtr3 = &Tic3;
	CharPtr4 = &Tic4;
	CharPtr5 = &Tic5;
	CharPtr6 = &Tic6;
	CharPtr7 = &Tic7;
	CharPtr8 = &Tic8;
	CharPtr9 = &Tic9;

	if (*CharPtr1 == 'X')
	{
		xCount++;
	}
	else if (*CharPtr1 == 'O')
	{
		oCount++;
	}
	
	if (*CharPtr2 == 'X')
	{
		xCount++;
	}
	else if (*CharPtr2 == 'O')
	{
		oCount++;
	}

	if (*CharPtr3 == 'X')
	{
		xCount++;
	}
	else if (*CharPtr3 == 'O')
	{
		oCount++;
	}

	if (*CharPtr4 == 'X')
	{
		xCount++;
	}
	else if (*CharPtr4 == 'O')
	{
		oCount++;
	}

	if (*CharPtr5 == 'X')
	{
		xCount++;
	}
	else if (*CharPtr5 == 'O')
	{
		oCount++;
	}

	if (*CharPtr6 == 'X')
	{
		xCount++;
	}
	else if (*CharPtr6 == 'O')
	{
		oCount++;
	}

	if (*CharPtr7 == 'X')
	{
		xCount++;
	}
	else if (*CharPtr7 == 'O')
	{
		oCount++;
	}

	if (*CharPtr8 == 'X')
	{
		xCount++;
	}
	else if (*CharPtr8 == 'O')
	{
		oCount++;
	}

	if (*CharPtr9 == 'X')
	{
		xCount++;
	}
	else if (*CharPtr9 == 'O')
	{
		oCount++;
	}

	if (xCount > oCount)
	{
		return PlayerMark = 'O';
	}
	else if (xCount == oCount)
	{
		return PlayerMark = 'X';
	}
}